package com.situ.shoplook.dubbo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.shoplook.common.model.Product;
import com.situ.shoplook.common.service.ProductService;
import com.situ.shoplook.common.util.PaginateInfo;
import com.situ.shoplook.dubbo.dao.ProductDao;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author:yzy
 * @date:2024/9/2
 */
@Service
@CacheConfig(cacheNames = "c.s.s.d.s.impl.ProductServiceImpl")
@DubboService
public class ProductServiceImpl implements ProductService {
    private ProductDao productDao;

    @Autowired
    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }

    @Cacheable(keyGenerator = "myKeyGenerator")
    @Override
    public Map<String, Object> findAll(PaginateInfo pi, String name) {
        try (Page<Product> __ = PageHelper.startPage(pi.getPageNo(), pi.getPageSize())) {
            List<Product> products = this.productDao.findAll(name);
            PageInfo<Product> pageInfo = new PageInfo<>(products);
            pi.setPageNo(pageInfo.getPageNum());
            pi.setPageSize(pageInfo.getPageSize());
            pi.setTotal((int)pageInfo.getTotal());
            products = List.copyOf(products);
            return Map.of( "data", products, "pi", pi);
        }
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean save(Product product) {
        return this.productDao.save(product) > 0;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean update(Product product) {
        return this.productDao.update(product) > 0;
    }

    @Transactional
    @CacheEvict(allEntries = true)
    @Override
    public int deleteByIds(Integer[] ids) {
        for (Integer id : ids){
            System.out.println(id);
        }
        return this.productDao.deleteByIds(ids);
    }

    @Override
    public List<Product> findById(Integer id) {
        return productDao.findById(id);
    }

    @Override
    public List<Product> findByName(String name) {
        return productDao.findByName(name);
    }

    @Override
    public List<Product> findAllProducts() {
        return productDao.findAll(null);
    }

    @Override
    public Product findOneProduct(Integer id) {
        return productDao.findOneProduct(id);
    }

    @Override
    public List<Product> findByIds(List<Integer> categoryIds) {
        List<Product> products = new ArrayList<>();
        for (Integer categoryId : categoryIds) {
            products.addAll(productDao.findByCategoryId(categoryId));
        }
        return products;
    }

    @Override
    public List<Product> findBest() {
        return productDao.findBest();
    }
}
